//
//  ShotImage.m
//  TheInterception
//
//  Created by Adinnet on 16/6/27.
//  Copyright © 2016年 Adinnet. All rights reserved.
//

#import "ShotImage.h"
#import <AssetsLibrary/AssetsLibrary.h>

@implementation ShotImage

//
static ShotImage *_shotImage = nil;

+ (instancetype)shareInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _shotImage = [[self alloc] init];
    });
    
    return _shotImage;
}

#pragma mark - 截取view中某个区域生成一张图片
- (UIImage *)shotWithView:(UIView *)view withScopeRect:(CGRect)inputRect {
    CGImageRef imageRef = CGImageCreateWithImageInRect([self shotWithView:view].CGImage, inputRect);
    UIGraphicsBeginImageContext(inputRect.size);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGRect rect = CGRectMake(0, 0, inputRect.size.width, inputRect.size.height);
    
    CGContextTranslateCTM(context, 0, rect.size.height);//下移
    CGContextScaleCTM(context, 1.0f, -1.0f);//上翻
    CGContextDrawImage(context, rect, imageRef);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    CGImageRelease(imageRef);
   
    return image;
}

#pragma mark - 截取view生成一张图片
- (UIImage *)shotWithView:(UIView *)view {
//    UIGraphicsBeginImageContext(view.bounds.size);
//    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
//    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
//    UIGraphicsEndImageContext();
//    return image;
    
    CGSize s = view.bounds.size;
    
    // 下面方法，第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果，需要传NO，否则传YES。第三个参数就是屏幕密度了，关键就是第三个参数。
    
    UIGraphicsBeginImageContextWithOptions(s, NO, [UIScreen mainScreen].scale);
    
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    
    UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return image;
}




@end
